package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

@WebServlet("/param")
public class ParamServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {

        // 解决参数乱码, 这个要在获取参数前设置
        request.setCharacterEncoding("utf-8");
        // 获取参数
//        System.out.println(request.getParameter("username"));
//        System.out.println(Arrays.toString(request.getParameterValues("hobby")));

//        Enumeration<String> parameterNames = request.getParameterNames();
//
//        Iterator<String> stringIterator = parameterNames.asIterator();
//
//        while ( stringIterator.hasNext() ){
//            String name = stringIterator.next();
//            System.out.println(name + "\t" + Arrays.toString(request.getParameterValues(name)));
//        }

        Map<String, String[]> m = request.getParameterMap();
        // keySet() values()  entrySet()
        Set<Map.Entry<String, String[]>> entrySet = m.entrySet();

        for (Map.Entry<String, String[]> entry : entrySet){
            System.out.println(entry.getKey() + "\t" + Arrays.toString(entry.getValue()));
        }

    }

}
